<template>
  <div class="userinfo">
    <el-card shadow="never">
      <template #header>
        <div class="header">
          <span>基本资料</span>
        </div>
      </template>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="登录名称" prop="username" >
          <el-input v-model="form.username" placeholder="请输入用户名" disabled style="width: 400px"/>
        </el-form-item>
        <el-form-item label="用户昵称" prop="nickname">
          <el-input v-model="form.nickname" placeholder="请输入内容" style="width: 400px"/>
        </el-form-item>
        <el-form-item label="用户邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" style="width: 400px" />
        </el-form-item>
        <el-form-item>
          <template>
            <el-button @click="submit" type="primary">提交修改</el-button>
            <el-button @click="reset" type="primary" plain>重置</el-button>
          </template>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { getUserinfo, updateUserinfo } from '@/api/user'
import ement from '@/components/ement.js'
export default {
  name: 'userinfo-page',
  data () {
    return {
      form: {
        id: '',
        username: '',
        nickname: '',
        email: ''
      },
      rules: {
        nickname: [
          { required: true, message: '请输入用户昵称', trigger: ['blur', 'change'] },
          { min: 1, max: 10, message: '请输入1~10个字符', trigger: ['blur', 'change'] }
        ],
        email: [{ required: true, message: '请输入用户邮箱', trigger: ['blur', 'change'] }]
      }
    }
  },
  async created () {
    this.initData()
  },
  methods: {
    async initData () {
      const { data } = await getUserinfo()
      this.form.username = data.username
      this.form.id = data.id
    },
    async submit () {
      await updateUserinfo(this.form)
      this.$message.success('修改用户信息成功')
      const { data } = await getUserinfo()
      this.$store.commit('user/getNickname', data.nickname)
      ement.$emit('find', data.nickname)
    },
    reset () {
      this.form.nickname = ''
      this.form.email = ''
    }
  }
}
</script>

<style lang="less" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
    span {
      font-size: 16px;
      color: #131523;
      font-weight: 500;
    }
  }
}
</style>
